<template>
	<view class="home">
		<!-- 轮播图 -->
		<swiper indicator-dots circular>
			<swiper-item v-for="(item, index) in swipers" :key="index">
				<image :src="item"></image>
			</swiper-item>
		</swiper>
		
		<!-- 导航区域 -->
		<view class='nav'>
			<view class='nav_item' v-for="(item, index) in navs" :key='index' @click='navItemClick(item.path)'>
				<view :class='item.icon'></view>
				<text>{{item.title}}</text>
			</view>
		</view>
		
		<!-- 推荐商品 -->
		<view class='hot_goods'>
			<view class='tit'>推荐商品</view>
			<goods-list :goods='goods' @goodsItemCLick='goGoodsDetail'></goods-list>
			</view>
		</view>
		
		
	</view>
	
</template>

<script>
	import goodsList from '../../components/goods-list/goods-list.vue'
	export default{
		data() {
			return {
				swipers: [],
				// page: 0,
				goods: [],
				navs: [
					{
						icon: 'iconfont icon-ziyuan',
						title: '博得超市',
						path: '/pages/goods/goods'
					},
					{
						icon: 'iconfont icon-guanyuwomen',
						title: '联系我们',
						path: '/pages/contact/contact'
					},
					{
						icon: 'iconfont icon-tupian',
						title: '社区图片',
						path: '/pages/pics/pics'
					},
					{
						icon: 'iconfont icon-shipin',
						title: '学习视频',
						path: '/pages/videos/videos'
					},
				]
			}
		},
		onLoad() {
			this.getSwipers()
			this.getHotGoods()
		},
		components: {'goods-list': goodsList},
		methods: {
			// 获取轮播图的数据
			async getSwipers() {
				// console.log('~~~~~获取轮播图的数据')
				const res = await this.$myRequest({
				  method: 'GET',
				  url: '/api/store/goods/',
				})
				if(res.statusCode !== 200){
					return uni.showToast({
						title: '获取数据失败!',
						icon: 'none'
					})
				}
				
				var key_name = 'image'
				var values = this.get_keyname_value(res.data, key_name)
				for(var i = 0; i < values.length; i++){
					 values[i] = this.$baseUrl + values[i]
				}
				
				this.swipers = values
			},
			
			// 获取推荐商品数据
			async getHotGoods () {
				const res = await this.$myRequest({
					url: '/api/store/goods/'
				})
				var new_data = this.add_hostname_before_keyname(res.data, 'image')
				this.goods = new_data
			},
			
			// 导航点击的处理函数
			navItemClick (url) {
				uni.navigateTo({
					url
				})
			},
			
			// 导航到商品详情页
			goGoodsDetail(id) {
				uni.navigateTo({
					url: "/pages/goods-detail/goods-detail?id=" + id
				})
			}
			
		}
	}
	
</script>

<style lang="scss">
	.home{
		swiper{
			width: 750rpx;
			height: 380rpx;
			image{
				height: 85%;
				width: 80%;
				margin-left: 10%;
			}
		}
		.nav {
			display: flex;
			.nav_item {
				width: 25%;
				text-align: center;
				view{
					width: 120rpx;
					height: 120rpx;
					background-color: #B50E03;
					border-radius: 60rpx;
					margin: 10px auto;
					line-height: 120rpx;
					color: #FFFFFF;
					font-size: 50rpx;
				}
				.icon-tupian{
					font-size: 45rpx;
				}
				text{
					font-size: 30rpx;
				}
			}
		}
		
		.hot_goods{
			background: #eee;
			overflow: hidden;
			margin-top: 10rpx;
			margin-top: 10px;
			.tit {
				height: 50px;
				line-height: 50px;
				color: $shop-color;
				text-align: center;
				letter-spacing: 20px;
				background: #FFFFFF;
				margin: 10rpx 0;
			}
		}
		
		
		
	}
</style>
